home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / chrome / content / related_articles.js < prev    next >
Text File  |  2010-01-12  |  5KB  |  133 lines

  1. //Note to editor: NOT executed from XUL
  2. //var smarterwiki_$ = SmarterWiki.$;
  3. //var smarterwiki_$ = jQuery.noConflict(true);
  4.  
  5. (function()
  6. {
  7.     //var $ = smarterwiki_$;
  8.     var getBoolPref = function(pref_name, callback)
  9.     {
  10.         if(callback)
  11.         {
  12.             callback(safe_SW_getBoolPref("extensions.smarterwiki." + pref_name));
  13.         }
  14.     };
  15.     if(window.chrome && window.chrome.extension) // detect chrome
  16.     {
  17.         var get_localStorage = function(key, callback) //only used for Chrome
  18.         {    
  19.             chrome.extension.sendRequest({"msg_type": "get_localStorage", "key": key}, function(response)
  20.             {
  21.                 if(callback)
  22.                 {
  23.                     callback(response.value);
  24.                 }
  25.             });
  26.         };
  27.         var set_localStorage = function(key, value, callback) //only used for Chrome
  28.         {    
  29.             chrome.extension.sendRequest({"msg_type": "set_localStorage", "key": key, "value": value}, function(response)
  30.             {
  31.                 if(callback)
  32.                 {
  33.                     callback(response.status);
  34.                 }
  35.             });
  36.         };
  37.  
  38.         setBoolPref = function(pref_name, value, callback)
  39.         {
  40.             set_localStorage("pref." + pref_name, value, callback);
  41.         };
  42.  
  43.         getBoolPref = function(pref_name, callback)
  44.         {
  45.             get_localStorage("pref." + pref_name, function(str)
  46.             {
  47.                 if(callback) {
  48.                     callback(str == "true");
  49.                 }
  50.             });
  51.         };
  52.         
  53.         SW_$ = $;
  54.         SW_LOG = function(){};
  55.         $.get = function(url, data, callback, type)
  56.         {
  57.             chrome.extension.sendRequest({"msg_type": "$.get", "url": url, "data": data, "type": type}, function(response)
  58.             {
  59.                 if(callback)
  60.                 {
  61.                     callback(response.data, response.textStatus);
  62.                 }
  63.             });            
  64.         };
  65.     }
  66.     
  67.     var safe_SW_getBoolPref = function(name)
  68.     {
  69.         try
  70.         {
  71.             return SW_getBoolPref(name);
  72.         }
  73.         catch(err)
  74.         {
  75.             return null;
  76.         }
  77.     };
  78.     
  79.     getBoolPref("add_related_articles", function(pref_value)
  80.     {
  81.         if(pref_value)
  82.         {
  83.             var doc = document;
  84.             var urlRegExp = new RegExp("(http(?:|s)://en.wikipedia.org/wiki/)(.*)");
  85.             var match = urlRegExp.exec(doc.location.href);
  86.             if(match != null)
  87.             {
  88.                 //on a Wikipedia page
  89.                 var wgPageName = match[2];//.replace("_", " ");
  90.                 var title_match = new RegExp("^(.*) - .*$").exec(doc.title);
  91.                 var wgArticleName = ""; //Main Page has no title in that format
  92.                 if(title_match)
  93.                 {
  94.                     wgArticleName = title_match[1];
  95.                 }
  96.                 var wgArticlePath = match[1];
  97.                 //var locale = SW_getCharPref("general.useragent.locale");
  98.                 var locale = navigator.language;
  99.  
  100.                 var addRelatedArticlesBox = function(html, textStatus)
  101.                 {
  102.                     var searchPortlet = $('#p-search', doc);
  103.  
  104.                     if(searchPortlet && searchPortlet.attr("class") == "portlet")
  105.                     {
  106.                         var relatedPortlet = $('<div id="p-smarterwiki"></div>', doc);
  107.                         relatedPortlet.addClass('portlet', doc);
  108.                         relatedPortlet.append(html);
  109.                         relatedPortlet.insertBefore(searchPortlet);  
  110.                         /*
  111.                         relatedPortlet.click(function()
  112.                         {
  113.  
  114.                         });
  115.                         */
  116.                     }
  117.                     else
  118.                     {
  119.                         var $navPortlet = $("#p-navigation");
  120.                         var $relatedPortlet = $('<div id="p-smarterwiki"></div>', doc).addClass('portal', doc).append(html).insertAfter($navPortlet);
  121.                         $relatedPortlet.find(".pBody").removeClass("pBody").addClass("body");  
  122.                         $relatedPortlet.find("h5").text("Related articles");  
  123.                     }
  124.                 };
  125.  
  126.                 SW_$.get("http://static.smarterfox.com/api/related_articles",
  127.                     {'topic': wgArticleName,
  128.                      'locale': locale,
  129.                      'format': 'html_frag'}, addRelatedArticlesBox, "html");
  130.             }
  131.         }
  132.     });
  133. }());